﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using IronOne.SalesOne.BusinessObjects;
using System.Configuration;

namespace IronOne.SalesOne.DataObjects
{
    public class LinqDependant
    {
        public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["GridCount"]);

        public List<Dependant> GetCustomers(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<DependentEntity> query = db.DependentEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "DepartmentID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.DependentID);
                            else
                                query = query.OrderByDescending(c => c.DependentID);
                            break;
                        case "CustomerID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.CustomerID);
                            else
                                query = query.OrderByDescending(c => c.CustomerID);
                            break;
                        case "Name":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.Name);
                            else
                                query = query.OrderByDescending(c => c.Name);
                            break;
                    }
                }
                return query.Select(c => Mapper.ToDependantObject(c)).ToList();
            }
        }


        public Dependant GetDependant(int dependantId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToDependantObject(db.DependentEntities
                            .SingleOrDefault(p => p.DependentID == dependantId));
            }
        }


        public List<Dependant> GetDependentsForCustomer(string customerId, int pageNo)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {

                IQueryable<DependentEntity> liability = from dep in db.DependentEntities
                                                        where dep.CustomerID == customerId
                                                        select dep;

                IQueryable<DependentEntity> pageData = liability.Skip((pageNo - 1) * pageSize).Take(pageSize);

                return pageData.Select(c => Mapper.ToDependantObject(c)).ToList();

            }

        }


        public void InsertDependant(Dependant dependant)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    DependentEntity entity = Mapper.ToDependantEntity(dependant);
                    db.DependentEntities.InsertOnSubmit(entity);
                    db.SubmitChanges();

                    dependant.DependantId= dependant.DependantId;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to dependant record was made before your changes.");
                }
                catch (Exception)
                {
                    throw new Exception("Unable to Insert dependant " + dependant.DependantId);
                }
            }
        }


        public int UpdateDependant(Dependant dependant)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    DependentEntity entity = Mapper.ToDependantEntity(dependant);
                    db.DependentEntities.Attach(entity, true);
                    db.SubmitChanges();
                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to dependant record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }


        public int DeleteCustomer(Dependant dependant)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                try
                {
                    DependentEntity entity = Mapper.ToDependantEntity(dependant);
                    db.DependentEntities.Attach(entity, false);
                    db.DependentEntities.DeleteOnSubmit(entity);
                    db.SubmitChanges();

                    return 1;
                }
                catch (ChangeConflictException)
                {
                    throw new Exception("A change to dependant record was made before your changes.");
                }
                catch (Exception)
                {
                    return 0;
                }
            }
        }
    
    
        //for NA dependents
        public List<NaDependent> GetNaDependents(string sortExpression)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                IQueryable<NADependentEntity> query = db.NADependentEntities;

                if (sortExpression.Length > 0)
                {
                    string[] sort = sortExpression.Split(' ');
                    string sortColumn = sort[0];
                    string sortOrder = sort[1];

                    switch (sortColumn)
                    {
                        case "DepartmentID":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.DependentId);
                            else
                                query = query.OrderByDescending(c => c.DependentId);
                            break;
                        
                        case "Name":
                            if (sortOrder == "ASC")
                                query = query.OrderBy(c => c.Name);
                            else
                                query = query.OrderByDescending(c => c.Name);
                            break;
                    }
                }
                return query.Select(c => Mapper.ToNaDependantObject(c)).ToList();
            }
        }


        public NaDependent GetNADependant(int dependantId)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {
                return Mapper.ToNaDependantObject(db.NADependentEntities
                            .SingleOrDefault(p => p.DependentId == dependantId));
            }
        }


        public List<NaDependent> GetDependentsForNa(int needAn, int pageNo)
        {
            using (SODataDataContext db = DataContextFactory.CreateContext())
            {

                IQueryable<NADependentEntity> nadep =   from dep in db.NADependentEntities
                                                        where dep.NeedsAnalysisNo == needAn
                                                        select dep;

                IQueryable<NADependentEntity> pageData = nadep.Skip((pageNo - 1) * pageSize).Take(pageSize);

                return pageData.Select(c => Mapper.ToNaDependantObject(c)).ToList();

            }

        }
    }
}
